package com.vv.tools.modules.utils.web.controller;

import com.vv.tools.common.message.Result;
import com.vv.tools.common.util.FreemarkerUtil;
import com.vv.tools.common.util.JsonUtil;
import com.vv.tools.modules.utils.api.request.FreemarkerRequest;
import com.vv.tools.modules.utils.api.request.QccRequest;
import com.vv.tools.modules.utils.api.response.QccProgressResponse;
import com.vv.tools.modules.utils.domain.service.QccService;
import com.vv.tools.modules.utils.domain.service.ReCaptchaService;
import com.vv.tools.modules.utils.web.controller.converter.UtilsConverter;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;

/**
 * @author liwen
 */
@RestController
@RequestMapping("/utils")
public class UtilController {

    private static final String STR_REGEX = "[\\n\\t]";

    @Resource
    private QccService qccService;

    @Resource
    private ReCaptchaService reCaptchaService;

    @PostMapping("/freemarker")
    public Result<String> freemarker(@RequestBody FreemarkerRequest request, HttpServletRequest req) {
        String s = request.getJsonTxt().replaceAll(STR_REGEX, "");
        return Result.success(FreemarkerUtil.parse(request.getFtlTxt(), JsonUtil.toObject(s, Map.class)));
    }

    @Operation(summary = "qcc", description = "qcc")
    @PostMapping("/address")
    public Result<String> qcc(@RequestBody QccRequest request, HttpServletRequest req) {
        return Result.success(qccService.getResult(UtilsConverter.INSTANCE.req2bo(request)));
    }

    @Operation(summary = "qcc", description = "qcc")
    @GetMapping("/address/{requestId}")
    public Result<QccProgressResponse> qccPro(@PathVariable("requestId") String requestId) {
        return Result.success(UtilsConverter.INSTANCE.bo2resp(qccService.getProgress(requestId)));
    }

}
